//
//  Tweet.m
//  oschina
//
//  Created by wangjun on 12-3-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Tweet.h"
#import "Tool.h"

@implementation Tweet

@synthesize _id;
@synthesize author;
@synthesize authorID;
@synthesize fromNowOn;
@synthesize img;
@synthesize imgData;
@synthesize commentCount;
@synthesize imgTweet;
@synthesize imgTweetData;
@synthesize imgBig;
@synthesize attach;
@synthesize appClient;
@synthesize height;

+ (id)tweetWithXMLElement:(TBXMLElement *)element
{
    TBXMLElement *_id;
    TBXMLElement *portrait;
    TBXMLElement *author;
    TBXMLElement *authorID;
    TBXMLElement *words;
    TBXMLElement *commentCount;
    TBXMLElement *pubDate;
    TBXMLElement *imgSmall;
    TBXMLElement *imgBig;
    TBXMLElement *appClient;
    Tweet *t;
    _id = [TBXML childElementNamed:@"id" parentElement:element];
    portrait = [TBXML childElementNamed:@"portrait" parentElement:element];
    author = [TBXML childElementNamed:@"author" parentElement:element];
    authorID = [TBXML childElementNamed:@"authorid" parentElement:element];
    words = [TBXML childElementNamed:@"body" parentElement:element];
    commentCount = [TBXML childElementNamed:@"commentCount" parentElement:element];
    pubDate = [TBXML childElementNamed:@"pubDate" parentElement:element];
    imgSmall = [TBXML childElementNamed:@"imgSmall" parentElement:element];
    imgBig = [TBXML childElementNamed:@"imgBig" parentElement:element];
    TBXMLElement *attach = [TBXML childElementNamed:@"attach" parentElement:element];
    appClient = [TBXML childElementNamed:@"appclient" parentElement:element];
    t = [[Tweet alloc] initWidthParameters:[[TBXML textForElement:_id] intValue] andAuthor:[TBXML textForElement:author] andAuthorID:[[TBXML textForElement:authorID] intValue] andTweet:[TBXML textForElement:words] andFromNowOn:[Tool intervalSinceNow:[TBXML textForElement:pubDate]] andImg:[TBXML textForElement:portrait] andCommentCount:[[TBXML textForElement:commentCount] intValue] andImgTweet:[TBXML textForElement:imgSmall] andImgBig:[TBXML textForElement:imgBig] andAppClient:[[TBXML textForElement:appClient] intValue] andAttach:[TBXML textForElement:attach]];
    return t;
}

+ (void)tweetListBy:(NSDictionary*)parameters block:(void (^)(NSArray *list, int total, NSError *error))block
{
    [[AFOSCClient sharedClient] getPath:api_tweet_list parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *response = operation.responseString;
        @try {
            
            TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil];
            int count = [Tool isListOver2:response];
            
            if (count == 0) {
                block([NSArray array], count, nil);
                return;
            }
            TBXMLElement *root = xml.rootXMLElement;
            //显示
            TBXMLElement *tweetlist = [TBXML childElementNamed:@"tweets" parentElement:root];
            int allCountTweets = [TBXML textForElement:[TBXML childElementNamed:@"tweetCount" parentElement:root]].intValue;
            
            TBXMLElement *first = [TBXML childElementNamed:@"tweet" parentElement:tweetlist];
            
            NSMutableArray *newTweets = [[NSMutableArray alloc] initWithCapacity:20];
            
            Tweet* t = [self tweetWithXMLElement:first];
            
            if (t) {
                [newTweets addObject:t];
            }
            
            
            while (first != nil)
            {
                first = [TBXML nextSiblingNamed:@"tweet" searchFromElement:first];
                if (first)
                {
                    Tweet* t = [self tweetWithXMLElement:first];
                    if (t) {
                        [newTweets addObject:t];
                    }
                }
                else
                {
                    break;
                }
            }
            block(newTweets, count, nil);
            
        }
        
        @catch (NSException *exception) {
            [NdUncaughtExceptionHandler TakeException:exception];
            NSError* error = [NSError errorWithDomain:@"oschina" code:0 userInfo:exception.userInfo];
            block([NSArray array], 0, error);
        }
        @finally {
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block([NSArray array], 0, error);
    }];
}

- (id)initWidthParameters:(int)newid
                andAuthor:(NSString *)newAuthor
              andAuthorID:(int)newAuthorID
                 andTweet:(NSString *)newTweet
             andFromNowOn:(NSString *)newFromNowOn
                   andImg:(NSString *)newImg
          andCommentCount:(int)newCommentCount
              andImgTweet:(NSString *)nimgTweet
                andImgBig:(NSString *)nimgBig
             andAppClient:(int)nappClient
                andAttach:(NSString *)nattach
{
    Tweet *t = [[Tweet alloc] init];
    t._id = newid;
    t.author = newAuthor;
    t.authorID = newAuthorID;
    t.tweet = newTweet;
    t.fromNowOn = newFromNowOn;
    t.img = newImg;
    t.commentCount = newCommentCount;
    t.imgTweet = nimgTweet;
    t.imgBig = nimgBig;
    t.appClient = nappClient;
    t.attach = nattach;
    UITextView *txt = [[UITextView alloc] initWithFrame:CGRectMake(157, 178, 236, 331)];
    t.height = [Tool getTextViewHeight:txt andUIFont:[UIFont fontWithName:@"arial" size:14.0f] andText:t.tweet];
    return t;
}

@end
